package com.itmk.web.teacher.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.student.entity.Student;
import com.itmk.web.teacher.entity.Teacher;
import com.itmk.web.teacher.entity.TeacherParm;
import com.itmk.web.teacher.service.TeacherService;
import com.itmk.web.teacher_role.entity.TeacherRole;
import com.itmk.web.teacher_role.service.TeacherRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author java实战基地
 * @Version 3501754007
 */
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private TeacherRoleService teacherRoleService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody Teacher teacher){
        //学号重复判断
        QueryWrapper<Teacher> query = new QueryWrapper<>();
        query.lambda().eq(Teacher::getTeacherNum,teacher.getTeacherNum());
        Teacher one = teacherService.getOne(query);
        if(one != null){
            return ResultUtils.error("教师编号被占用!");
        }
        //设置密码
        teacher.setPassword(DigestUtils.md5DigestAsHex(teacher.getPassword().getBytes()));
        teacherService.addTeacher(teacher);
        return ResultUtils.success("新增成功！");
    }

    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody Teacher teacher){
        //学号重复判断
        QueryWrapper<Teacher> query = new QueryWrapper<>();
        query.lambda().eq(Teacher::getTeacherNum,teacher.getTeacherNum());
        Teacher one = teacherService.getOne(query);
        if(one != null && !teacher.getTeacherId().equals(one.getTeacherId())){
            return ResultUtils.error("教师编号被占用!");
        }
        teacherService.editTeacher(teacher);
        return ResultUtils.success("编辑成功！");
    }

    //删除
    @DeleteMapping("/{teacherId}")
    public ResultVo delete(@PathVariable("teacherId") Long teacherId){
        teacherService.deleteTeacher(teacherId);
        return ResultUtils.success("删除成功！");
    }

    //列表查询
    @GetMapping("/list")
    public ResultVo getList(TeacherParm parm){
        IPage<Teacher> list = teacherService.getList(parm);
        return ResultUtils.success("查询成功",list);
    }

    //根据教师id查询教师信息
    @GetMapping("/getTeacher")
    public ResultVo getTeacher(Long teacherId){
        Teacher teacher = teacherService.getById(teacherId);
        QueryWrapper<TeacherRole> query = new QueryWrapper<>();
        query.lambda().eq(TeacherRole::getTeacherId,teacherId);
        TeacherRole one = teacherRoleService.getOne(query);
        teacher.setRoleId(one.getRoleId());
        return ResultUtils.success("查询成功",teacher);
    }

    //教师重置密码
    @PostMapping("/resetPassword")
    public ResultVo resetPassword(@RequestBody Teacher teacher){
        //设置密码
        teacher.setPassword(DigestUtils.md5DigestAsHex("666666".getBytes()));
        teacherService.updateById(teacher);
        return ResultUtils.success("重置密码成功！");
    }
}
